<?php
/**
 * Status class, holds status data.
 * @package status
 * @author alcaitiff
 */
class Status extends ExportableObject
{
	protected $id,$duration,$capabilities,$description,$commands,$level,$applied;
	private $done;
	/**
     *
     * @param string $id Name
     * @param int $level Level
     * @param int $applied Timestamp
     */
	function __construct ($id,$level,$applied=null){
		$this->id=$id;
		$this->level=$level;
		$this->applied=$applied;
		$this->capabilities=array();
		$this->commands=array();
		$this->done=false;
		if($this->id!=null){
			$this->retrieveData();
			$this->retrieveCapabilities();
			$this->retrieveCommands();
		}
	}
    /**
     * Verify if status is done
     * @return boolean
     */
	public function isDone(){
		return $this->done;
	}
    /**
     * Set done as true
     * @return void
     */
	public function done(){
		$this->done=true;
	}
    //***********************************************************************************************************
    //					DATABASE METHODS
    //***********************************************************************************************************
	/**
     * Retrieve DB data
     * @return void
     */
	private function retrieveData(){
		
		$dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'SELECT  `description`, '.
										'`duration` '.
								'FROM `status` '.		
								'WHERE   `status` = ? '.
								'LIMIT 1'
								);
	    $stmt->bind_param("s", $this->id);
	    $stmt->bind_result($this->description,$this->duration);
	    $stmt->execute();
	    $stmt->fetch();
  		$stmt->close();
  		
	}
    /**
     * Retrieve capabilities
     * @return void
     */
	private function retrieveCapabilities(){
		
		$dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'SELECT  `capability`, '.
										'`value` '.
								'FROM `status_capabilities` '.		
								'WHERE   `status` = ? '
								);
	    $stmt->bind_param("s", $this->id);
	    $stmt->bind_result($cap,$val);
	    $stmt->execute();
	    while($stmt->fetch()){
	    	$this->capabilities[$cap]=$val;
	    }
  		$stmt->close();
  		
	}
    /**
     * Retrieve commands
     * @return void
     */
	private function retrieveCommands(){
		$dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'SELECT  `command`, '.
										'`value` '.
								'FROM `status_commands` '.		
								'WHERE   `status` = ? '
								);
	    $stmt->bind_param("s", $this->id);
	    $stmt->bind_result($com,$val);
	    $stmt->execute();
	    while($stmt->fetch()){
	    	$this->commands[$com]=$val;
	    }
  		$stmt->close();

	}
}